<template>
  <div class="table-box">
    <ProTable ref="proTable" :pagination="false" :columns="columns" :request-api="getTableList" :init-param="initParam">
      <!-- 表格 header 按钮 -->
      <template #tableHeader>
        <el-button type="primary" :icon="CirclePlus" @click="openDrawer(OperatorType.ADD)">新增</el-button>
      </template>
      <!-- 表格操作 -->
      <template #operation="scope">
        <el-button type="primary" link :icon="View" @click="openDrawer(OperatorType.VIEW, scope.row)">查看</el-button>
        <el-button type="primary" link :icon="EditPen" @click="openDrawer(OperatorType.EDIT, scope.row)">编辑</el-button>
        <el-button type="primary" link :icon="Delete" @click="openDrawer(OperatorType.DELETE, scope.row)">删除</el-button>
      </template>
    </ProTable>
    <detail ref="drawerRef" />
  </div>
</template>

<script setup lang="ts" name="ModConfig">
import { useRoute } from "vue-router";
import { useTabsStore } from "@/stores/modules/tabs";
import { ref, reactive } from "vue";
import { Mod } from "@/api/interface";
import { useHandleData } from "@/hooks/useHandleData";
import ProTable from "@/components/ProTable/index.vue";
import detail from "@/views/mod/def/detail.vue";
import { ProTableInstance, ColumnProps } from "@/components/ProTable/interface";
import { CirclePlus, Delete, EditPen, View } from "@element-plus/icons-vue";
import { getModColumnList, deleteModColumn, editModColumn, addModColumn } from "@/api/modules/mod";
import { OperatorType } from "@/enums/enum";

const route = useRoute();
const tabStore = useTabsStore();
tabStore.setTabsTitle(`${route.meta.title}(${route.query.modName})`);

// ProTable 实例
const proTable = ref<ProTableInstance>();

// 如果表格需要初始化请求参数，直接定义传给 ProTable (之后每次请求都会自动带上该参数，此参数更改之后也会一直带上，改变此参数会自动刷新表格数据)
const initParam = reactive({});

// 如果你想在请求之前对当前请求参数做一些操作，可以自定义如下函数：params 为当前所有的请求参数（包括分页），最后返回请求列表接口
// 默认不做操作就直接在 ProTable 组件上绑定	:requestApi="getUserList"
const getTableList = (params: any) => {
  console.log(params);
  params = { modId: route.params.id };
  return getModColumnList(params);
};

// 表格配置项
const columns = reactive<ColumnProps<Mod.SysModColumn>[]>([
  { type: "index", label: "#", width: 80 },
  {
    prop: "columnName",
    label: "字段名",
    width: 160
  },
  {
    prop: "columnLabel",
    label: "字段中文名",
    width: 120
  },
  {
    prop: "dataType",
    label: "数据类型"
  },
  {
    prop: "controlType",
    label: "控件类型"
  },
  {
    prop: "dataSource",
    label: "数据源"
  },
  {
    prop: "isListColumn",
    label: "是否列表字段"
  },
  {
    prop: "isSortColumn",
    label: "是否排序字段"
  },
  {
    prop: "isDefaultSortColumn",
    label: "是否默认排序字段"
  },
  {
    prop: "queryType",
    label: "查询类型"
  },
  {
    prop: "sumType",
    label: "汇总类型"
  },
  { prop: "operation", label: "操作", fixed: "right", width: 230 }
]);

// modName:模块名称
const modName = "字段";

// 打开 drawer(新增、查看、编辑)
const drawerRef = ref<InstanceType<typeof detail> | null>(null);

// type:查看、新增、修改、删除、配置
const openDrawer = async (type: OperatorType, row: Partial<Mod.SysModColumn> = { modId: route.params.id as string }) => {
  if (type === OperatorType.DELETE) {
    await useHandleData(deleteModColumn, row, `删除【${row.columnName}】${modName}`);
    proTable.value?.getTableList();
    return;
  }

  const params = {
    title: type + modName,
    type: type,
    row: { ...row },
    api: type === OperatorType.ADD ? addModColumn : type === OperatorType.EDIT ? editModColumn : undefined,
    getTableList: proTable.value?.getTableList
  };
  drawerRef.value?.acceptParams(params);
};
</script>
